package com.cms.demo.advice;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author wangning
 * @create 2021-04-26 10:48
 */
@ControllerAdvice
public class CustomExceptionHandler {
	@ExceptionHandler(MaxUploadSizeExceededException.class)
	public void uploadException(MaxUploadSizeExceededException e, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html;charset=utf-8");
		int status = resp.getStatus();
		System.out.println("status = " + status);
		PrintWriter writer = resp.getWriter();
		writer.write(status);
		writer.write("上传文件大小超出限制！");
		writer.flush();
		writer.close();
	}
}
